package com.demo.androidgame.finsternis;

import java.util.List;

import javax.microedition.khronos.opengles.GL10;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;

import com.demo.androidgame.fx.Game;
import com.demo.androidgame.fx.Input.KeyEvent;
import com.demo.androidgame.fx.Input.TouchEvent;
import com.demo.androidgame.fx.gl.Camera2D;
import com.demo.androidgame.fx.gl.SpriteBatcher;
import com.demo.androidgame.fx.impl.GLGame;
import com.demo.androidgame.fx.impl.GLScreen;
import com.demo.androidgame.fx.math.OverlapTester;
import com.demo.androidgame.fx.math.Rectangle;
import com.demo.androidgame.fx.math.Vector2;

public class MainMenuScreen extends GLScreen {
    Camera2D guiCam;
    SpriteBatcher batcher;
    Rectangle startBounds;
    Rectangle creditBounds;
    Rectangle helpBounds;
    Rectangle soundBounds;
    Rectangle storyBounds;
    Vector2 touchPoint;
    
    public MainMenuScreen(Game game) {
        super(game);
        guiCam = new Camera2D(glGraphics, 480, 320);
        batcher = new SpriteBatcher(glGraphics, 100);
        
        startBounds = new Rectangle(480 - 130, 240, 90, 60);
        creditBounds = new Rectangle(480 - 130, 170, 90, 60);
        helpBounds = new Rectangle(480 - 130, 100, 90, 60);
        soundBounds = new Rectangle(480 - 130, 20, 90, 60);
        storyBounds = new Rectangle(150, 145, 50, 50);

        touchPoint = new Vector2();     
        
    }       

    @Override
    public void update(float deltaTime) {
        List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
        
        //backButton
        List<KeyEvent> keyEvents = game.getInput().getKeyEvents();
        int key = keyEvents.size();
        for (int i = 0; i <key; i++) {
        	KeyEvent event2 = keyEvents.get(i);
        	if (event2.type == KeyEvent.KEY_DOWN){	
	        	if (event2.keyCode == android.view.KeyEvent.KEYCODE_BACK) {
	        		((Activity)game).runOnUiThread(new Runnable() {
						@Override
						public void run() {
							new AlertDialog.Builder((Activity)game)
		        			.setIcon(R.drawable.finsternis)
		        			.setTitle("Finsternis")
		        			.setMessage("게임을 종료하시겠습니까?")
		        			.setCancelable(true)
		        			.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog, int which) {
									((Activity)game).finish();
								}
		        			}).setNegativeButton("No", null).show();
						}
					});
	        	}
        	}
        }
        
        
        int len = touchEvents.size();
        for(int i = 0; i < len; i++) {
            TouchEvent event = touchEvents.get(i);                        
            if(event.type == TouchEvent.TOUCH_UP) {
                touchPoint.set(event.x, event.y);
                guiCam.touchToWorld(touchPoint);
                
                if(OverlapTester.pointInRectangle(startBounds, touchPoint)) {
                    Assets.playSound(Assets.clickSound);
                    game.setScreen(new StageScreen(game));	//! test
                    return;
                }
                if(OverlapTester.pointInRectangle(creditBounds, touchPoint)) {
                    Assets.playSound(Assets.clickSound);
                    game.setScreen(new CreditScreen(game));
                    return;
                }
                if(OverlapTester.pointInRectangle(helpBounds, touchPoint)) {
                    Assets.playSound(Assets.clickSound);
                    game.setScreen(new HelpScreen(game));
                    return;
                }
                if(OverlapTester.pointInRectangle(soundBounds, touchPoint)) {
                    Assets.playSound(Assets.clickSound);
                    Settings.soundEnabled = !Settings.soundEnabled;
                    if(Settings.soundEnabled) {
                    	Assets.gameMusic.play();
                    	Assets.mainMusic.play();
                    }else {
                        Assets.gameMusic.pause();
                        Assets.mainMusic.pause();
                    }
                }
                if(OverlapTester.pointInRectangle(storyBounds, touchPoint)) {
                    Assets.playSound(Assets.clickSound);
                    game.setScreen(new StoryScreen(game));
                    return;
                }
            }
            
        }
        
    }

    @Override
    public void present(float deltaTime) {
        GL10 gl = glGraphics.getGL();        
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        guiCam.setViewportAndMatrices();
        
        gl.glEnable(GL10.GL_TEXTURE_2D);
        
        batcher.beginBatch(Assets.mainscreen);
        batcher.drawSprite(240, 160, 480, 320, Assets.mainscreenRegion);
        batcher.endBatch();
        
        gl.glEnable(GL10.GL_BLEND);
        gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);   
        
        
        batcher.beginBatch(Assets.mainscreenmenu);                 
        
        batcher.drawSprite(480 - 90, 270, 80, 26, Assets.start);
        batcher.drawSprite(480 - 90, 200, 80, 26, Assets.credit);
        batcher.drawSprite(480 - 90, 125, 80, 26, Assets.manual);
        batcher.drawSprite(480 - 90, 50, 80, 26, Settings.soundEnabled?Assets.soundon:Assets.soundoff);
        
        batcher.endBatch();
        
        gl.glDisable(GL10.GL_BLEND);
        
        if(Settings.soundEnabled) {
			Assets.mainMusic.play();
		}
        Assets.gameMusic.pause();
      
        gl.glDisable(GL10.GL_LIGHT1);
		gl.glDisable(GL10.GL_LIGHTING);
    }
    
    @Override
    public void pause() {        
        Settings.save(game.getFileIO());
    }

    @Override
    public void resume() {        
    }       

    @Override
    public void dispose() {        
    }
}
